# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the BugPrediction Bugzilla Extension.
#
# The Initial Developer of the Original Code is Tom Wijsman
# Portions created by the Initial Developer are Copyright (C) 2010 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Tom Wijsman <TomWij [at sign] live [dot character] com>

package Bugzilla::Extension::BugPrediction;
use base qw(Bugzilla::Extension);

use DBI;
use DBD::mysql;
use CGI;

# This code for this is in ./extensions/BugPrediction/lib/Util.pm
use Bugzilla::Extension::BugPrediction::Util;

our $VERSION = '0.01';

# See the documentation of Bugzilla::Hook ("perldoc Bugzilla::Hook" 
# in the bugzilla directory) for a list of all available hooks.

# Update the database so that a statistics table is included.
sub install_update_db {
    my ($self, $args) = @_;
	
	do 'lib\updateDatabase.cgi';
}

# Update the id of the user when a new bug has been submitted.
sub bug_end_of_create {
    my ($self, $args) = @_;
	
	# Get information about the user and the bug.
	$CGIquery = new CGI;
	$cookie = $CGIquery->cookie('Bugzilla_logincookie');

    $bug = $args->{'bug'};
    $bid = $bug->id;
	$pid = $bug->product;
	$cid = $bug->component;

	# Update the statistics in MySQL.
	do 'localconfig';
	$driver = DBI->install_driver("mysql"); 
	$connect = $driver->connect("DBI:mysql:database=" . $db_name . ";host=" . $db_host, $db_user, $db_pass, {}) or print "<b>MySQL: Connection failed.</b>";

	$query = "UPDATE suggestions SET bid = '$bid', pid = '$pid', cid = '$cid' WHERE bid = '-1' AND user = '$cookie';";
	$query_handle = $connect->prepare($query) or warn "<b>MySQL: Query Preparation failed.</b>";
	$query_handle->execute() or warn "<b>MySQL: Query Execution failed.</b>";
}

__PACKAGE__->NAME;